package pl.mdziedzic.unittest;


import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class seleniumBase {
	public  static WebDriver driver;
	private static StringBuffer verificationErrors = new StringBuffer();

	
	
	@BeforeClass
	public static void setUp() throws Exception {
		driver = new FirefoxDriver();
		
		//driver = new HtmlUnitDriver();
		driver.manage().deleteAllCookies();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		//driver.get("http://code.google.com/p/selenium/");
		
	}
	
	
	@AfterClass
	public static void tearDown() throws Exception {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	public void assertTextPresent(String text){
		try {
			assertTrue(driver.getPageSource().contains(text));
		} catch (Exception e) {
			
			verificationErrors.append(e.toString());
			e.printStackTrace();
		}
	}
	
	public boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
//Implememnted additional methods - that should make code easier to read
	
	public void click(By identifier) {
		driver.findElement(identifier).click();
	}
	public void sendKeys(By identifier, String text) {
		driver.findElement(identifier).sendKeys(text);
	}

	public void clear(By identifier) {
		driver.findElement(identifier).clear();
	}
}
